//
// File:        rm_record.cc
// Description: rm_record class implementation
// Authors:     Haris Wang (dynmiw@gmail.com)
//
// 
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include "rm.h"
#include "rm_rid.h"


RM_Record::RM_Record():recordSize(-1), data(NULL), rid(-1,-1){}


RM_Record::~RM_Record()
{
    if (data != NULL)
        delete [] data;
}


// Return the data corresponding to the record.  Sets *pData to the
// record contents.
RC RM_Record::GetData(char *&pData) const
{
    if (data==NULL | recordSize==-1)
    {
        return (START_RM_ERR-2);   
    }

    pData = data;
    return 0;
}


// Return the RID associated with the record
RC RM_Record::GetRid (RID &rid) const
{
    if (data==NULL | recordSize==-1)
    {
        return (START_RM_ERR-2);   
    }

    rid = this->rid;
    return 0;
}


//Sets data in the record for a fixed record-size
RC RM_Record::Set(char *pData, int size, RID rid_)
{
    if(recordSize!=-1 && (size!=recordSize))
    {
        return (START_RM_ERR - 3);
    }
    recordSize = size;

    this->rid = rid_;
    if(data==NULL)
        data = new char[recordSize];
    memcpy(data, pData, size);
    return 0;
}